﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using EOS.Framework.Web.Mvc.Rendering;

namespace EOS.Framework.Web.Mvc.Controls
{
    public class Image : Control
    {
        private string _alt;

        public string Alt
        {
            get { return _alt; }
            set { _alt = value; }
        }

        private string _file;

        public string File
        {
            get { return _file; }
            set { _file = value; }
        }

        public Image(string file)
            : this(file, null)
        { }

        public Image(string file, string alt)
        {
            this._file = file;
            this._alt = alt;
        }

        protected override void  OnRender(IXhtmlWriter writer, PageContext context)
        {
            if (TagName.ToLower(CultureInfo.CurrentCulture).Equals("img", StringComparison.CurrentCulture) == false)
            {
                throw new ControlMarkupException(
                    string.Format(CultureInfo.CurrentCulture, Properties.Resources.InvalidTagName, "img"));
            }

            if (string.IsNullOrEmpty(_file))
            {
                throw new ArgumentException("file");
            }

            if (string.IsNullOrEmpty(_alt))
            {
                WriteBeginAttachedTag(writer, new string[] { "src", "alt" });
                writer.WriteAttribute("alt", _alt);
            }
            else
            {
                WriteBeginAttachedTag(writer, new string[] { "src" });
            }

            writer.WriteAttribute("src", ToAbsolutePath(_file));

            WriteEndAttachedTag(writer);
        }
    }
}
